package cn.iocoder.yudao.module.bk.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bk.controller.admin.lwbook.vo.LwBookPageReqVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbook.vo.LwBookRespVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply.vo.BkImportExcelVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply.vo.BkImportRespVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply.vo.LwBookInApplyRes;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookoutapply.vo.LwBookOutApplyRes;
import cn.iocoder.yudao.module.bk.controller.app.vo.*;
import cn.iocoder.yudao.module.bk.dal.dataobject.lwbook.LwBookDO;
import cn.iocoder.yudao.module.bk.service.lwbook.LwBookService;
import cn.iocoder.yudao.module.bk.util.BarcodeReader;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author Administrator
 */
@Tag(name = "app - 来往书籍")
@RestController
@RequestMapping("/bk/lw-book")
@Validated
public class AppBookController {

    @Resource
    private LwBookService lwBookService;

    @PostMapping("/nowInfo")
    @Operation(summary = "当前状态")
    public CommonResult<HomeInfoRes> nowInfo() {
        return lwBookService.nowInfo();
    }

    @PostMapping("/bookInApply")
    @Operation(summary = "新增书籍飘入")
    public CommonResult<?> bookInApply(@Valid @RequestBody PutBookInReq req) {
        return lwBookService.bookInApply(req);
    }

    @PostMapping("/bookOutApply")
    @Operation(summary = "新增书籍飘出")
    public CommonResult<?> bookOutApply(@Valid @RequestBody PutBookOutReq req) {
        return lwBookService.bookOutApply(req);
    }

    @PostMapping("/myInApplies")
    @Operation(summary = "我的飘入申请")
    public CommonResult<List<LwBookInApplyRes>> myInApplies() {
        return lwBookService.myInApplies();
    }

    @PostMapping("/myOutApplies")
    @Operation(summary = "我的飘出申请")
    public CommonResult<List<LwBookOutApplyRes>> myOutApplies() {
        return lwBookService.myOutApplies();
    }

    @PostMapping("/isbnSearch")
    @Operation(summary = "isbn查询")
    public CommonResult<BookInfoReq> isbnSearch(@Valid @RequestBody IsbnReq req) {
        return lwBookService.isbnSearch(req);
    }

    @GetMapping("/newBooks")
    @Operation(summary = "最新书籍")
    public CommonResult<List<NewBookRes>> newBooks() {
        return lwBookService.newBooks();
    }

    @GetMapping("/getInLwBookPage")
    @Operation(summary = "获得来往书籍分页")
    public CommonResult<PageResult<LwBookRespVO>> getLwBookPage(@Valid LwBookPageReqVO pageReqVO) {
        PageResult<LwBookDO> pageResult = lwBookService.getInLwBookPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LwBookRespVO.class));
    }

    @PostMapping("/uploadAndReadISBN")
    @Operation(summary = "上传图片并识别ISBN")
    public CommonResult<String> uploadAndReadISBN(@RequestParam("file") MultipartFile file) {
        try {
            return CommonResult.success(BarcodeReader.readBarcodeFromMuFile(file));
        } catch (Exception e) {
            return CommonResult.error("无法识别ISBN");
        }
    }

    @PostMapping("/uploadAndSearchBookInfo")
    @Operation(summary = "上传图片并搜索书籍信息")
    public CommonResult<BookInfoReq> uploadAndSearchBookInfo(@RequestParam("file") MultipartFile file) {
        try {
            // 识别ISBN
            String isbn = BarcodeReader.readBarcodeFromMuFile(file);
            // 调用isbnSearch接口获取书籍信息
            IsbnReq isbnReq = new IsbnReq();
            isbnReq.setIsbn(isbn);
            return lwBookService.isbnSearch(isbnReq);
        } catch (Exception e) {
            return CommonResult.error("无法识别ISBN或获取书籍信息");
        }
    }

}
